#coding:utf-8
# 处理依赖数据
# sys.path.append('F:/A_RuanJian/Py_project/imooc/')
from Z_other.unit_for_excel.operation_excel import OperationExcel
# from data.runmethod import RunMethod
from Z_other.other.get_data import GetData
from jsonpath_rw import parse
# from util.operation_json import OperetionJson

class DependdentData:
	def __init__(self,case_id):
		self.case_id = case_id
		self.data = GetData()
		self.opera_excel = OperationExcel()

	# 通过case_id去获取该case_id的整行数据
	def get_case_line_data(self):
		rows_data = self.opera_excel.get_rows_data(self.case_id)
		return rows_data

	# 执行依赖测试，并判断是否需要添加cookie，然后获取结果
	# def run_dependent(self):
	# 	run_method = RunMethod()
	# 	row_num  = self.opera_excel.get_row_num(self.case_id)
	#
	# 	request_data = self.data.get_data_for_json(row_num)
	# 	cookie = self.data.is_cookie(row_num)
	# 	method = self.data.get_request_method(row_num)
	# 	url = self.data.get_request_url_host(row_num)
	# 	headers = self.data.get_header_for_json(row_num)
	# 	header_type = self.data.get_header_type(row_num)
	#
	# 	if header_type == 'json':
	# 		request_data = json.dumps(request_data)
	# 	else:
	# 		pass
	# 	if cookie == 'yes':
	# 		op_json = OperetionJson(Path.path_cookie)
	# 		cookie= op_json.get_data('__outh')
	# 		cookies = {'__outh':cookie}
	# 		res = run_method.run_main(method,url,request_data,headers,cookies)
	# 	else:
	# 		res = run_method.run_main(method,url,request_data,headers)
	# 	a01 = res.json()
	# 	# print(header_type);print(type(request_data))
	# 	return a01

	# 根据依赖的key去获取,并执行依赖测试case的响应,然后返回
	def get_data_for_key(self,row):
		depend_data = self.data.get_depend_key(row)
		response_data = self.run_dependent()
		try:
			male = parse(depend_data).find(response_data)
			a01 = [match.value for match in male][0]
		except:
			# print (json.dumps(response_data, ensure_ascii=False))
			print ('错误：依赖数据获取失败')
		else:
			# print (a01)
			return a01

	def get_de_data(self,row,request_data):
		depend_response_data = self.get_data_for_key(row)
		# 发起前置请求，并获取返回的依赖key: 获取将key分隔，并经过转格式后的list；然后获取list的长度

		int_list = self.data.get_depend_field(row)
		# 获取数据依赖字段-list（key）
		len01 = len(int_list)
		# 根据处理后的key数据，更新后置请求的数据，目前支持的索引最大深度为3
		if len01 == 1:
			b01 = int_list[0]
			request_data[b01] = depend_response_data
		elif len01 == 2:
			# b01 = int_list[0];b02 = int_list[1]
			b01,b02 = int_list
			request_data[b01][b02] = depend_response_data
		elif len01 == 3:
			# b01 = int_list[0];b02 = int_list[1];b03 = int_list[2]
			b01,b02,b03 = int_list
			request_data[b01][b02][b03] = depend_response_data
		return request_data




